Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
nodejs [] operator, avoid picking first char of array of strings

I have an array of strings that sometimes is an array that only contains one string. when that is the case array[0] does not return the whole string but returns the first char in the string.

I can get around it with some if checking if length is > 1 but it is awful.

is there a safe [] operator or method that always return the whole string in position 0 of the array instead of the first char in the string?

This is an express input-based array in an html form. I don't really have too much control over how it's generated.

i can't use a foreach as I have other parallel arrays from that form.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Before operating your var as an array, you should verify that it is an array.

In you case, i guess that when you get only a string the provider of your data return a string type and when there is more than one string it return an array of string.

So you need to verify the type returned by your data provider and then process with the right type.

if ( Array.isArray(yourVar) ) {
  console.log('My var is an array');
} else {
  console.log('My var is not an array');
}

You can also use typeof to check your var type, for example verify that your var is a string.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda